<script setup name="BizRefundOrderIndex">
import eventBus from "@/utils/eventBus";
import { usePagination } from "@/utils/composables";
import FormMode from "./modules/FormMode/index.vue";
import { parseTime, selectDictLabel, addDateRange } from "@/utils/aidex";
import { listBizRefundOrder, wxRefunds } from "@/api/biz/bizRefundOrder";
import { getDicts } from "@/api/system/dict/data";
const { pagination, getParams, onSizeChange, onCurrentChange, onSortChange } =
  usePagination();
const router = useRouter();

const data = ref({
  loading: false,
  /**
   * 详情展示模式
   * router 路由跳转
   * dialog 对话框
   * drawer 抽屉
   */
  formMode: "drawer",
  // 详情
  formModeProps: {
    visible: false,
    id: ""
  },
  // 搜索
  search: {
    status: "",
    searchDate: [],
    orderId: ""
  },
  // 批量操作
  batch: {
    enable: false,
    selectionDataList: []
  },
  // 列表数据
  dataList: [],
  refundStatusOptions: {}
});

// 页面挂载时 调用
onMounted(() => {
  getDataList();
  if (data.value.formMode === "router") {
    eventBus.on("get-data-list", () => {
      getDataList();
    });
  }
  getDicts("refund_order_status").then((res) => {
    data.value.refundStatusOptions = res.data;
  });
});
//
onBeforeUnmount(() => {
  if (data.value.formMode === "router") {
    eventBus.off("get-data-list");
  }
});

// 每页数量切换
function sizeChange(size) {
  onSizeChange(size).then(() => getDataList());
}

// 当前页码切换（翻页）
function currentChange(page = 1) {
  onCurrentChange(page).then(() => getDataList());
}

// 字段排序
function sortChange(prop, order) {
  onSortChange(prop, order).then(() => getDataList());
}
// 查询
function getDataList() {
  data.value.loading = true;
  let params = getParams();
  data.value.search.orderId && (params.orderId = data.value.search.orderId);
  data.value.search.status && (params.status = data.value.search.status);
  listBizRefundOrder(addDateRange(params, data.value.search.searchDate)).then(
    (res) => {
      data.value.loading = false;
      data.value.dataList = res.data.list;
      pagination.value.total = res.data.total;
    }
  );
}
function refundStatusOptionsFormat(row) {
  if (row.status) {
    return selectDictLabel(data.value.refundStatusOptions, row.status);
  } else {
    return "";
  }
}
// 添加
function onAdd() {
  data.value.formModeProps.id = "";
  data.value.formMode = "dialog";
  data.value.formModeProps.visible = true;
}

function onOkRefund(orderId) {
  wxRefunds({ orderId: orderId }).then((res) => {
    if (res.code == 200) {
      ElMessage.success({
        message: "退款成功",
        center: true
      });
      getDataList();
    } else {
      ElMessage.error(res.msg);
    }
  });
}
const cancelEvent = () => {
  console.log("cancel!");
};
</script>

<template>
  <div>
    <page-main style="height: calc(100vh - 160px); margin: 10px">
      <search-bar>
        <el-form
          :model="data.search"
          size="default"
          label-width="auto"
          label-suffix=""
        >
          <el-row :gutter="30">
            <el-col :span="5">
              <el-form-item label="订单编号">
                <el-input
                  v-model="data.search.orderId"
                  placeholder="请输入订单编号"
                  clearable
                  @keydown.enter="currentChange()"
                  @clear="currentChange()"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="订单时间">
                <el-date-picker
                  v-model="data.search.searchDate"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  value-format="YYYY-MM-DD"
                />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="订单状态">
                <el-select v-model="data.search.status">
                  <el-option
                    :key="index"
                    v-for="(item, index) in data.refundStatusOptions"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                    >{{ item.dictLabel }}</el-option
                  >
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="5">
              <el-form-item>
                <el-button type="primary" @click="currentChange()">
                  <template #icon>
                    <el-icon>
                      <svg-icon name="ep:search" />
                    </el-icon>
                  </template>
                  查询
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </search-bar>

      <div style="height: calc(100vh - 380px); overflow-y: auto">
        <el-table
          ref="table"
          v-loading="data.loading"
          class="list-table"
          :data="data.dataList"
          border
          highlight-current-row
          row-key="id"
          @sort-change="sortChange"
          @selection-change="data.batch.selectionDataList = $event"
        >
          >
          <el-table-column type="selection" align="center" />
          <el-table-column
            type="index"
            align="center"
            label="序号"
            width="60"
          />

          <el-table-column
            prop="orderId"
            label="订单编号"
            show-overflow-tooltip
            align="right"
            width="220"
          >
            <template v-slot="scope">
              {{ scope.row.orderId }}
            </template>
          </el-table-column>
          <el-table-column
            prop="orderType"
            label="订单类型"
            show-overflow-tooltip
            align="center"
            width="100"
          >
            <template v-slot="scope">
              <el-tag v-if="scope.row.orderType == 0">洗护订单</el-tag>
              <el-tag v-if="scope.row.orderType == 1">商城订单</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="refundMoeny"
            label="退款金额"
            show-overflow-tooltip
            align="right"
            width="120"
          >
            <template v-slot="scope">
              ¥ {{ parseFloat(scope.row.refundMoeny).toFixed(2) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="申请时间"
            show-overflow-tooltip
            align="center"
            width="200"
          >
            <template v-slot="scope">
              {{ scope.row.createTime }}
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            label="状态"
            show-overflow-tooltip
            align="center"
            width="100"
          >
            <template v-slot="scope">
              <el-tag type="warning" v-if="scope.row.status == 0">{{
                refundStatusOptionsFormat(scope.row)
              }}</el-tag>
              <el-tag type="success" v-if="scope.row.status == 'SUCCESS'">{{
                refundStatusOptionsFormat(scope.row)
              }}</el-tag>
              <el-tag
                type="error"
                v-if="
                  scope.row.status == 'ABNORMAL' || scope.row.status == '-1'
                "
                >{{ refundStatusOptionsFormat(scope.row) }}</el-tag
              >
              <el-tag type="" v-if="scope.row.status == 'PROCESSING'">{{
                refundStatusOptionsFormat(scope.row)
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="wxRefundId"
            label="退款单号"
            show-overflow-tooltip
            align="center"
            width="200"
          >
            <template v-slot="scope">
              {{ scope.row.wxRefundId }}
            </template>
          </el-table-column>

          <el-table-column
            prop="channel"
            label="退款渠道"
            show-overflow-tooltip
            align="center"
            width="200"
          >
            <template v-slot="scope">
              {{ scope.row.channel }}
            </template>
          </el-table-column>
          <el-table-column
            prop="wxRefundTime"
            label="退款时间"
            show-overflow-tooltip
            align="center"
            width="200"
          >
            <template v-slot="scope">
              {{ scope.row.wxRefundTime }}
            </template>
          </el-table-column>
          <el-table-column
            prop="remark"
            label="备注"
            show-overflow-tooltip
            align="center"
            width="200"
          >
            <template v-slot="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            width="160"
            align="center"
            fixed="right"
          >
            <template #default="scope">
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="InfoFilled"
                icon-color="#626AEF"
                title="确定审核改退款申请？"
                @confirm="onOkRefund(scope.row.orderId)"
                @cancel="cancelEvent"
              >
                <template #reference>
                  <el-button type="primary" size="small" text>
                    <template #icon>
                      <el-icon>
                        <svg-icon name="ep:setting" />
                      </el-icon>
                    </template>
                    审核退款</el-button
                  >
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-pagination
        :current-page="pagination.page"
        :total="pagination.total"
        :page-size="pagination.size"
        :page-sizes="pagination.sizes"
        :layout="pagination.layout"
        :hide-on-single-page="false"
        class="pagination"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
      />
    </page-main>
    <FormMode
      v-if="['dialog', 'drawer'].includes(data.formMode)"
      :id="data.formModeProps.id"
      v-model="data.formModeProps.visible"
      :mode="data.formMode"
      @success="getDataList"
    />
  </div>
</template>

<style lang="scss" scoped>
.el-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
